package auctionServer.eventService;

import auctionServer.io.IOManager;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 * Created with IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class EventServerManager extends UnicastRemoteObject implements OuterServer {

    private int registryPort;
    private String registryHost;

    public EventServerManager()  throws RemoteException {
        // Assign a security manager, in the event that dynamic
        // classes are loaded
        /*
        SECURITY MANAGER IS CANCELED BY LAB ADVISOR
         */
//        if (System.getSecurityManager() == null)
//            System.setSecurityManager ( new RMISecurityManager() );

        java.io.InputStream is = ClassLoader.getSystemResourceAsStream("registry.properties");
        if (is != null) {
            java.util.Properties props = new java.util.Properties();
            try {
                props.load(is);
                registryHost = props.getProperty("registry.host");
                registryPort = Integer.parseInt(props.getProperty("registry.port"));
            } catch (IOException e) {
                IOManager.throwWarning("registry.properties file error");
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        } else {
            System.err.println("Properties file not found!");
        }

        //todo delete here after ant build
        registryPort = 20012;
        registryHost = "localhost";

        java.rmi.registry.LocateRegistry.createRegistry(registryPort);
        Registry registry = java.rmi.registry.LocateRegistry.getRegistry(registryHost, registryPort);
        // ... and bind it with the RMI Registry
        try {
            registry.rebind("OuterServer", this);
        } catch (RemoteException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return;
        }

        System.out.println ("Service bound....");
    }


    public void realeseConnection() {
        try {
            UnicastRemoteObject.unexportObject(this, true);
        } catch (NoSuchObjectException e) {
        }
    }


    @Override
    public void connectAnalyticsServer(String ip) throws RemoteException{
        //To change body of implemented methods use File | Settings | File Templates.
        System.out.println(ip);
    }

    @Override
    public void connectBiddingServer(String ip) throws RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
        System.out.println(ip);
    }
}
